﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using University.Workshop.Model;
using University.Workshop.DataAccess;
using University.Workshop.Business;
using System.Web.Security;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.ObjectModel; 

namespace University.Workshop.WebApp
{
   
    public partial class UserProducts : System.Web.UI.Page
    {
        public String IdProduct
        {
            get
            {
                return LblProductIdSelect.Text;
            }
        }

        Collection<Product> products;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!User.Identity.IsAuthenticated)
            {
                FormsAuthentication.RedirectToLoginPage();
            }
            products = new Collection<Product>();
            try
            {
                products = ProductDataAccess.GetProductsBySeller(UserDataAccess.GetUserByEmail(User.Identity.Name).Id, true);
                //GridView1.Columns[0].Visible = false; //lo siento chicos pero si no muestro el id no puedo conseguirlo lo toma nulo
                GridView1.DataSource = products;
                GridView1.DataBind();
                
                if (products.Count == 0)
                {
                    LblError.Visible = true;
                    LblError.Text = "You have no products on Sale or on Auction!";
                }
            }
            catch (Exception er)
            {
                LblError.Visible = true;
                LblError.Text = er.Message;
            }
        }

        protected void GridView1_SelectedIndexChanged(Object sender, EventArgs e)
        {
            try
            {
                GridViewRow row = GridView1.SelectedRow;
                LblProductIdSelect.Text =row.Cells[1].Text;
                LblProductName.Text = row.Cells[2].Text;
                if (row.Cells[6].Text == "OnSale")
                {
                    BtnAuction.Enabled = true;
                    BtnOffers.Enabled = true;
                    BtnCancelAuction.Enabled = false;
                }
                else
                {
                    if (row.Cells[6].Text == "OnAuction")
                    {
                        BtnCancelAuction.Enabled = true;
                        BtnAuction.Enabled = false;
                    }
                }
            }
            catch
            {
                LblError.Visible = true;
                LblError.Text="No rows selected";
            }
        }

        protected void BtnAuction_Click(object sender, EventArgs e)
        {
            Response.Redirect("RegisterAuction.aspx?IdProduct="+LblProductIdSelect.Text+"&NameProduct="+LblProductName.Text);
            Response.Write("deberia cargar datos");
        }

        protected void BtnCancelAuction_Click(object sender, EventArgs e)
        {
            if (products != null && GridView1.SelectedIndex >= 0)
            {
                Product selectedProduct = products[GridView1.SelectedIndex];
                Response.Redirect("ViewAuction.aspx?id=" + selectedProduct.ProductId + "&name=" + selectedProduct.Name);
            }
            else Response.Write("<script>alert('You must select one product!');</script>");
        }

        protected void BtnOffers_Click(object sender, EventArgs e)
        {
            Response.Redirect("MyOffers.aspx?IdProduct=" + LblProductIdSelect.Text);
        }

        protected void GridView1_PageIndexChanged(object sender, EventArgs e)
        {
            
        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex= e.NewPageIndex;
            GridView1.DataSource = products;
            GridView1.DataBind();
        }
    }
}